package whu.edu.assignment4.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import whu.edu.assignment4.dataobject.Product;
import whu.edu.assignment4.dataobject.ReturnObj;
import whu.edu.assignment4.dataobject.UserProduct;
import whu.edu.assignment4.enums.ReturnEnums;
import whu.edu.assignment4.service.ProductService;
import whu.edu.assignment4.util.ReturnUtil;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/findById")
    public ReturnObj findById(@RequestParam int pid) {
        Product product = productService.findProductById(pid);
        if(product == null) {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
        else {
            return ReturnUtil.success(product);
        }
    }

    @GetMapping("/list")
    public ReturnObj findAll() {
        List<Product> products = productService.findAll();
        if(products.size() <= 0) {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
        else {
            return ReturnUtil.success(products);
        }
    }

    @PostMapping("/buy")
    public ReturnObj buyProducts(@RequestBody UserProduct userProduct) {
        if(userProduct == null || userProduct.getNumber() <= 0) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        int result = productService.buyProducts(userProduct);
        switch (result) {
            case -1:
                return ReturnUtil.error(ReturnEnums.DataNotFound);
            case -2:
                return ReturnUtil.error(ReturnEnums.LessBalance);
            case 1:
                return ReturnUtil.success();
            default:
                return ReturnUtil.error(ReturnEnums.DataSaveError);
        }
    }

    @PostMapping("")
    public ReturnObj addProduct(@RequestBody Product product) {
        if(product == null) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        int result = productService.addProduct(product);
        if(result == 1) {
            return ReturnUtil.success();
        }
        else {
            return ReturnUtil.error(ReturnEnums.DataSaveError);
        }
    }

    @DeleteMapping("/{pid}")
    public ReturnObj deleteProduct(@PathVariable int pid) {
        if(pid <= 0) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        int result = productService.deleteProduct(pid);
        if(result == 1) {
            return ReturnUtil.success();
        }
        else {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
    }

    @PutMapping("/{pid}")
    public ReturnObj updateProduct(@PathVariable int pid, @RequestBody Product product) {
        if(pid <= 0) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        product.setPid(pid);
        int result = productService.updateProduct(product);
        if(result == 1) {
            return ReturnUtil.success();
        }
        else {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
    }
}
